package com.donggua.aichebao.commonui.shop;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.donggua.aichebao.commonui.R;

/**
 * Created by fzh on 2017/8/7.
 */

public class ShopPhotoLayout extends LinearLayout{
    private ImageView imageView;
    private TextView textView;
    private int margin = 15;
    private int textSizeType = TypedValue.COMPLEX_UNIT_PX;
    public ShopPhotoLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        margin = (int)context.getResources().getDimension(R.dimen.photoMargin);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomPhoto);
        this.setOrientation(LinearLayout.VERTICAL);
        this.setGravity(Gravity.CENTER_HORIZONTAL);
        addImageView(context,ta);
        addTextView(context,ta);
    }

    private void addTextView(Context context,TypedArray ta){
        textView = new TextView(context);
        String text = ta.getString(R.styleable.CustomPhoto_customPhotoText);
        int textSize = (int)ta.getDimension(R.styleable.CustomPhoto_customPhotoTextSize,15);
        int color = ta.getColor(R.styleable.CustomPhoto_customPhotoTextColor,0x77000000);
        textView.setText(text);
        textView.setTextColor(color);
        textView.setTextSize(textSizeType,textSize);
        textView.setGravity(Gravity.CENTER);
        LinearLayout.LayoutParams layoutParams = getMyLayoutParams();
        textView.setLayoutParams(layoutParams);
        addView(textView);
    }

    private void addImageView(Context context,TypedArray ta ){
        imageView = new ImageView(context);
        Drawable drawable = ta.getDrawable(R.styleable.CustomPhoto_customPhotoDrawable);
        imageView.setImageDrawable(drawable);
        LinearLayout.LayoutParams layoutParams = getMyLayoutParams();
        imageView.setLayoutParams(layoutParams);
        this.addView(imageView);
    }
    private LinearLayout.LayoutParams getMyLayoutParams(){
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        return layoutParams;
    }
}
